/*
	JavaScript Document
*/

// Initial and infinite scroll loading of images.
var images_loading = true;
var loader = "<img src=\"public/images/loader.gif\" class=\"loader\">";
var $loader = $(loader);
var loader_mini = "<img src=\"public/images/loader_mini.gif\" class=\"loader\">";
var $loader_mini = $(loader_mini);

// Auto-scroll.
var positionOfElement;
var presentLocation;

// Masonry.
var $content = $('div#content');
var $modules = $('.module');
var col_w = 205;

// AJAX/Loading.
var objects_loading = false;
var timeout= 300;
var $post_buffer = $('#post_buffer');

$(document).ready(function() {
	
	// Content is loading.
	$content.prepend($loader);
	
	// Wait for all images in the content div to load, then fire callback.
	$($content).onImagesLoad({ 
  	selectorCallback: initialImagesLoaded,
  	callbackIfNoImagesExist: true
  });
  
  $('.module.normal:not(.filtered)').live('click',function(){
  
  	var $this = $(this);
		var t = $this.attr("id");
		
		$this.find('.overlay').hide();
		
		$this.addClass('loading');
		
		// Image loaded callback for AJAX loading of full content.
		function imagesLoaded(){
			$this.removeClass('normal').removeClass('loading').addClass('expanded');
			if($this.hasClass('course, resource, news')){
				$('.meta', this).show();
			}
			$this.find('p.content').html($post_buffer.html());
			$this.mouseover();
			$post_buffer.empty();
			$this.imageSet(function(){
				$content.masonry('reload');
				$this.scrollToTop();
			});
		}
		
		/* ***** Will!!! Replaced .load with .post span.info,this returned null obj.
				 uses the t variable as a reference to the target masonry widget and 
				 initialises both the buffer and the content of the block. */
		$.post("./ajaxy/getBlock/" + $this.find('.ajxInfo').html(), {bdata: $this.find('.ajxInfo').html()}, function(data){
			data = $.parseJSON(data);
			if(data.content != 'na'){
				$this.find('span.buffer').html($this.find('p.content').html());
				$post_buffer.html(data.content);
				//$this.find('p.content').html(data.content);
				if(data.content.indexOf('<object')> -1){
					objects_loading = true;
					window.setTimeout(check_for_objects, timeout);
				}
				$post_buffer.onImagesLoad({ 
					selectorCallback: imagesLoaded,
					callbackIfNoImagesExist: true
				});
			} 
		});
		
		// Close all of the expanded modules and replace summary content.
		$this.siblings('.expanded').each(function(){
			$(this).removeClass('expanded').addClass('normal');
			$content.masonry('reload');
			if($this.hasClass('course, resource, news')){
				$('.meta', this).hide();
			}
			$('.overlay', this).show();
			/* ***** Will!!! If a block is open, it will have data in it's buffer.
			 * 				 Load that data in and display it as the html if the targets
			 * 				 buffer actually has something in it.
			 */
			if($(this).find('span.buffer').html() != ""){
				$(this).find('p.content').html($(this).find('span.buffer').html());
			}
		});

	});
	
	$('ul#navigation li:not(#search) a').click(function(event) {
		
		event.preventDefault();
		
		var $this = $(this).parents('li');
		var typeofcontent = '.' + $this.attr('id');
		
		if($this.hasClass('selected')){
			$this.removeClass('selected');
			$('.module.filtered').removeClass('filtered').css('opacity', 1);
		}else{
			$this.addClass('selected');
			$this.siblings().removeClass('selected');
			$('.module.filtered').removeClass('filtered').css('opacity', 1);
			$('.module.normal, .module.expanded').not(typeofcontent).addClass('filtered').css('opacity', .25);
			$('.module.tweet').addClass('filtered').css('opacity', .25);
		}
		
	});
  
  // Show close icon when hovering over expanded modules.
  $('.module.expanded').live('mouseover mouseout', function(event) {
    if(event.type == 'mouseover'){
      $('.icon', this).addClass('close');
    }else{
      $('.icon', this).removeClass('close');
    }
  });
  
  // Close an expanded module, return the module summary and reload masonry.
  $('.icon.close').live('click',function(){
  	var $module = $(this).parents('.module');
  	$(this).removeClass('close');
  	$module.removeClass('expanded').addClass('normal');
  	objects_loading = false;
  	$module.find("p.content").html($module.find("span.buffer").html());
  	$module.find('.meta').hide();
  	$module.find('.overlay').show();
  	$content.masonry('reload');
  });
  
  // Scroll to and highlight the login box on clicking the login link.
  $('a#loginAnchor').click(function(event){
  	event.preventDefault();
  	var $siblings = $modules.not('#login, .logo');
  	$siblings.css('opacity', '0.25');
  	$('#login').scrollToTop(500, function(){
  		setTimeout(function(){
  			$siblings.animate({opacity:1}, 2000);
  		}, 2000);
  	});
  });
  
  $("div#login form input#submit").live('click',function(event){
  	event.preventDefault();
  	performPost('logIn', this);
  });
  
  $("#UserFrmSubmit").live('click',function(event){
  	event.preventDefault();
  	performPost('sendInfo', this);
  });
  
  var clicked = false;
  $('a#shotcodelogo').click(function(e){
  	
  	e.preventDefault();
  	var $this = $(this).find('img');
  	
  	if(!clicked){
  		clicked = true;
	  	$this.animate({width: 300, height: 300, "padding-left":27}, 1000, 'easeInOutQuad', function(){
	  		$content.masonry('reload');
	  	});
	  }else{
	  	clicked = false;
	  	$this.animate({width: 111, height: 111, "padding-left":0}, 1000, 'easeInOutQuad', function(){
	  		$content.masonry('reload');
	  	});
	  }
	  
  });
		
});

// Images loaded callback (first load).
function initialImagesLoaded(){
	$loader.remove();
	$content.masonry({
		itemSelector: '.module',
		columnWidth: col_w,
		resizeable: true
	});
	$modules.animate({opacity:1}, 1000);
}

function check_for_objects(){
	if(objects_loading == true){
		var oHeight = $('object','p.content').height();
		if(oHeight > 0){
			$content.masonry('reload');
			objects_loading = false;
		}else{
			window.setTimeout(check_for_objects, timeout);
		}
		
	}
}

function performPost(cmd, _this){
	var fdata = $('div#login form').serializeArray();
	var orig = $('div#login').html();
	$('div#login').html($loader_mini);
	$.post("ajaxy/" + cmd, {frmdata: fdata}, function(data){
			data = $.parseJSON(data);
			if(data.success == true && data.banned == false){
				$('div#login').html(data.form).addClass('expanded');
				$content.masonry('reload');
			}else{
				$('div#login').html(orig);
				_this.click();
				$content.masonry('reload');
			}
	});
}